//
//  CXBroadcastViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXBroadcastViewController.h"

#import "CXTitleView.h"
#import "CXBroadcastCollectionViewCell.h"
#import "CXStationCollectionViewCell.h"
#import "CXStationCategoryCollectionViewCell.h"
#import "CXBroadcastViewModel.h"


static NSString * const broadCell = @"broadCell";

@interface CXBroadcastViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, CXTitleViewDelegate, CXStationButtonDelegate>

@property (nonatomic, strong) CXBroadcastViewModel * broadModel;

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) UIButton * selectBtn;

@property (nonatomic, assign) CGRect rect;

@property (nonatomic, strong) CXStationCollectionViewCell * stationCell;

@end

@implementation CXBroadcastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:243 / 255.0 blue:247 / 255.0 alpha:1.0];
    //头部刷新
    [self.collectionView.mj_header beginRefreshing];
}


//头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
          viewForSupplementaryElementOfKind:(NSString *)kind
                                atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UICollectionReusableView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                 withReuseIdentifier:@"newHeadView"
                                                                                        forIndexPath:indexPath];
//        CXStationCollectionViewCell * stationCell = [[CXStationCollectionViewCell alloc] initWithFrame:headView.frame];
//        stationCell.delegate = self;
//        self.stationCell.frame = headView.bounds;
        self.stationCell.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:243 / 255.0 blue:247 / 255.0 alpha:1.0];
        [headView addSubview:self.stationCell];
        return headView;
    }else {
        UICollectionReusableView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                                 withReuseIdentifier:@"UICollectionViewHeader" forIndexPath:indexPath];
        if (headView != nil) {
            CXTitleView * moreView = [[CXTitleView alloc] initWithTitle:[self.broadModel mainTitleWithSection:indexPath.section] hasMore:[self.broadModel mainTitleWithSection:indexPath.section]];
            moreView.delegate = self;
            moreView.tag = indexPath.section;
            moreView.frame = headView.bounds;
            [headView addSubview:moreView];

        }
        return headView;
    }
}


//设置section
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(CXScreenWidth, 100);
    }else {
        return CGSizeMake(0, 40);
    }
}


//设置cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(CXScreenWidth / 4 - 6, 50);
    }
    return CGSizeMake(CXScreenWidth, 90);
}


#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.selectBtn.selected == YES) {
            return 8;
        }
        return self.broadModel.categoriesCount;
    }
    return 3;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CXStationCategoryCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StationCategoriesCell" forIndexPath:indexPath];
        
        [cell.stationButton setTitle:[self.broadModel nameWithIndex:indexPath.item] forState:UIControlStateNormal];
        self.selectBtn.frame = cell.frame;
        
        cell.backgroundColor = [UIColor lightGrayColor];
        return cell;
    }else if(indexPath.section == 1) {
        CXBroadcastCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:broadCell forIndexPath:indexPath];
        
        [cell.coverLargeImageView setImageWithURL:[self.broadModel coverURLWithSection:indexPath.section index:indexPath.item] placeholderImage:kGetImage(@"cell_bg_noData_1")];
        cell.nameLabel.text = [self.broadModel nameWithSection:indexPath.section index:indexPath.item];
        
        cell.programNameLabel.text = [self.broadModel programNameWithSection:indexPath.section index:indexPath.row];
        [cell.playButton setTitle:[NSString stringWithFormat:@"%ld万人",[self.broadModel playCountWithSection:indexPath.section index:indexPath.item] / 10000] forState:UIControlStateNormal];
        
        return cell;
    }else {
        CXBroadcastCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:broadCell forIndexPath:indexPath];
        [cell.coverLargeImageView setImageWithURL:[self.broadModel coverURLWithSection:indexPath.section index:indexPath.item] placeholderImage:kGetImage(@"cell_bg_noData_1")];
        cell.nameLabel.text = [self.broadModel nameWithSection:indexPath.section index:indexPath.item];
        cell.programNameLabel.text = [self.broadModel programNameWithSection:indexPath.section index:indexPath.row];
        [cell.playButton setTitle:[NSString stringWithFormat:@"%ld万人",[self.broadModel playCountWithSection:indexPath.section index:indexPath.item] / 10000] forState:UIControlStateNormal];
        
        return cell;
    }
}



//点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld", indexPath.section);
    NSLog(@"%ld", indexPath.row);
}


#pragma mark - CXTitleViewDelegate
-(void)didClicktitleView:(NSInteger)tag {
    
}


#pragma mark - CXStationButtonDelegate
-(void)didClickStationButton:(UIButton *)button {
    if (button.tag == 1001) {
        NSLog(@"1");
    }else if(button.tag == 1002) {
        NSLog(@"2");
    }else if(button.tag == 1003) {
        NSLog(@"3");
    }else if(button.tag == 1004) {
        NSLog(@"4");
    }
}



#pragma mark - lazy load method
-(UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        flowLayout.minimumLineSpacing = 1;
        flowLayout.minimumInteritemSpacing = 1;
        
        flowLayout.headerReferenceSize = CGSizeMake(CXScreenWidth, 100);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 15, 10);
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CXScreenWidth, CXScreenHeight - 49 - 64) collectionViewLayout:flowLayout];
        [self.view addSubview:collectionView];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXStationCategoryCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"StationCategoriesCell"];
        
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXBroadcastCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:broadCell];
        
        collectionView.bounces = YES;
        
        self.collectionView = collectionView;
        
        //上拉刷新
        collectionView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            
            [self.broadModel getDataCompletionHandle:^(NSError *error) {
                [self.collectionView.mj_header endRefreshing];
                [self.collectionView reloadData];
            }];
        }];
        
        //注册头视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"newHeadView"];
    }
    return _collectionView;
}

-(CXBroadcastViewModel *)broadModel {
    if (!_broadModel) {
        _broadModel = [[CXBroadcastViewModel alloc] init];
    }
    return _broadModel;
}


-(UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [[UIButton alloc] init];
        [_selectBtn setImage:kGetImage(@"icon_radio_hide") forState:UIControlStateNormal];
        [_selectBtn setImage:kGetImage(@"icon_radio_show") forState:UIControlStateSelected];
        [_selectBtn setBackgroundColor:[UIColor lightGrayColor]];
        [_selectBtn addTarget:self action:@selector(didClickBtn) forControlEvents:UIControlEventTouchUpInside];
        self.selectBtn.selected = YES;
        [self.collectionView addSubview:_selectBtn];
    }
    return _selectBtn;
}


-(void)didClickBtn {
    self.selectBtn.selected = !self.selectBtn.selected;
    [self.collectionView reloadData];
}

- (CXStationCollectionViewCell *)stationCell {
    if (!_stationCell) {
        _stationCell = [[CXStationCollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, 100)];
        _stationCell.delegate = self;
    }
    return _stationCell;
}

@end
